import sys
from PySide6.QtWidgets import QApplication, QPushButton, QMainWindow, QWidget, QVBoxLayout, QLabel
from random import randint

class AnotherWindow(QWidget):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()
        self.label = QLabel("Another Window % d" % randint(0, 100))
        layout.addWidget(self.label)
        self.setLayout(layout)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.w = None
        self.setWindowTitle("My App")
        
        button = QPushButton("Press me for a window!")
        button.clicked.connect(self.button_clicked)

        self.setCentralWidget(button)

    def button_clicked(self):
        if self.w is None:
            self.w = AnotherWindow()
            self.w.show()
        else:
            self.w.close()
            self.w = None

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()